package com.cxd.app.service;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.cxd.app.entity.WdtyInvest;
import com.cxd.app.entity.WdtyLoan;
import com.cxd.app.entity.base.LoanTypeBase;
import com.cxd.app.mapper.read.RLoanMapper;
import com.cxd.base.common.constants.RepayConstants;
import com.cxd.base.common.exception.CxdException;
import com.cxd.base.page.PageUtil;
import com.cxd.base.page.tag.TagPage;
import com.cxd.base.util.ArithUtil;
import com.cxd.base.util.BeanTools;
import com.cxd.base.util.ParamUtil;
import com.cxd.base.util.date.DateStyle;
import com.cxd.base.util.date.DateUtil;
import com.cxd.message.exception.AppConstants;

/**
 * 网贷天眼 
 * @author wtj
 * @date 2015年12月4日 下午3:00:39
 * @company CXD
 * @version [Copyright (c) 2015 V001]
 */
@Service("wdtyService")
public class WdtyService {
	
	@Resource
	public BeanTools bt;
	
	@Resource
	public LoanService loanService;

	@Resource
	public RLoanMapper rloanMapper;
	
	/**
	 * 网贷天眼获取借款信息
	 * @author wtj
	 * @date 2016年5月4日下午3:48:25
	 * @param json
	 * @param param
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 */
	public void wdty_loans(JSONObject json,Map<String, Object> param)
			throws InstantiationException, IllegalAccessException {
		
		Object pageSize = param.get("pageSize");
		Object currentPage = param.get("currentPage");
		if (pageSize == null || currentPage == null) {
			throw new CxdException("分页信息不能为空");
		}
		TagPage page = PageUtil.getTagPage(currentPage.toString(),pageSize.toString());
		param.put("page", page);
		
		List<Map<String,Object>> list = rloanMapper.wdtyLoansListPage(param);
		List<WdtyLoan> wdtyLoans = new ArrayList<WdtyLoan>();
		
		String status = param.get("status").toString();
		for(Map<String,Object> m:list){
			String loanId = m.get("id").toString();
			Double loanMoney = (Double)m.get("loan_money");
			LoanTypeBase loanType = loanService.queryLoanTypeByType(m.get("type").toString());
			
			WdtyLoan wl = new WdtyLoan();
			wl.setId(loanId);
			wl.setPlatform_name("诚信贷");
			wl.setUrl(MessageFormat.format(AppConstants.PublicConstants.WWW_LOAN_URL,loanId));
			wl.setTitle(m.get("name").toString());
			wl.setUserid(m.get("user_id").toString());
			wl.setUsername(m.get("user_id").toString());
			wl.setStatus(status);
			if(RepayConstants.RepayUnit.DAY.equals(loanType.getRepayTimeUnit())){
				wl.setP_type("0"); // 0 代表天
				wl.setC_type("6");
				wl.setPay_way("3");// 按天计息,一次性还本付息;
			}else{
				wl.setP_type("1"); // 1 代表月
				wl.setC_type("2");
				wl.setPay_way("2"); // 按月付息,到期还本;
			}
			wl.setAmount(loanMoney.toString());
			wl.setRate(m.get("rate").toString());
			wl.setPeriod(m.get("deadline").toString());
			
			List<Map<String,Object>> investList = loanService.queryLoanInvestSuccess(loanId);
			Double iAllMoney = 0d;
			for(Map<String,Object> im : investList){
				iAllMoney += (Double)im.get("money");
			}
			iAllMoney = iAllMoney > loanMoney?loanMoney:iAllMoney;
			wl.setProcess((ArithUtil.round(iAllMoney/loanMoney,4))+"");
			wl.setInvest_num(investList.size());
			wl.setStart_time(DateUtil.stringToString(m.get("commit_time").toString(),DateStyle.YYYY_MM_DD_HH_MM_SS.getValue()));
			if(status.equals("0")){
				// 借款起息时间
				wl.setEnd_time(DateUtil.stringToString(m.get("expect_time").toString(),DateStyle.YYYY_MM_DD_HH_MM_SS.getValue()));
			}else{
				// 放款时间
				wl.setEnd_time(DateUtil.stringToString(m.get("give_money_time").toString(),DateStyle.YYYY_MM_DD_HH_MM_SS.getValue()));
			}
			wdtyLoans.add(wl);
		}
		if(wdtyLoans.size()==0){
			json.put("result_code", "-1");
			json.put("result_msg", "未授权的访问");
			json.put("page_count", 0);
			json.put("page_index", 0);
			json.put("loans",null);
		}else{
			json.put("result_code", "1");
			json.put("result_msg", "获取数据成功");
			json.put("page_count", page.getTotalPage());
			json.put("page_index", param.get("currentPage"));
			json.put("loans",wdtyLoans);
		}
	}
	
	/**
	 * 获取借款投资记录
	 * @author wtj
	 * @date 2016年5月4日下午4:23:53
	 * @param json
	 * @param param
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 */
	public void wdty_invests(JSONObject json,Map<String, Object> param)
			throws InstantiationException, IllegalAccessException {
		Object pageSize = param.get("pageSize");
		Object currentPage = param.get("currentPage");
		Object loanId = param.get("loanId");
		ParamUtil.notNullParam("借款ID:" + loanId);
		if (pageSize == null || currentPage == null) {
			throw new CxdException("分页信息不能为空");
		}
		TagPage page = PageUtil.getTagPage(currentPage.toString(),pageSize.toString());
		param.put("page", page);
		List<Map<String,Object>> investList = loanService.queryLoanInvestSuccess(loanId.toString());
		
		List<WdtyInvest> wiList = new ArrayList<WdtyInvest>();
		for(Map<String,Object> im : investList){
			Double money = (Double)im.get("money");
			if(money<10){
				continue;
			}
			WdtyInvest wi = new WdtyInvest();
			wi.setId(loanId.toString());
			wi.setLink(MessageFormat.format(AppConstants.PublicConstants.WWW_LOAN_URL,loanId));
			wi.setUsername(im.get("user_id").toString());
			wi.setUserid(im.get("user_id").toString());
			wi.setType(im.get("is_auto_invest").equals("1")?"自动":"手动");
			wi.setMoney(money);
			wi.setAccount(im.get("money").toString());
			wi.setStatus("成功");
			wi.setAdd_time(DateUtil.stringToString(im.get("time").toString(),DateStyle.YYYY_MM_DD_HH_MM_SS.getValue()));
			wiList.add(wi);
		}
		json.put("result_code", "1");
		json.put("result_msg", "获取数据成功");
		json.put("page_count", page.getTotalPage());
		json.put("page_index", param.get("currentPage"));
		json.put("loans",wiList);
	}
}
